package lumis.service.mailmarketing;

import lumis.content.ContentStringResource;
import lumis.content.table.ContentTableDeleteDataProcessActionHandler;
import lumis.doui.source.TabularData;
import lumis.portal.PortalException;

/**
 * Deletes the user subscription and subscription categories
 *
 * @version $Revision: 5284 $ $Date: 2006-12-11 15:43:40 -0200 (Mon, 11 Dec 2006) $
 * @since 4.0.6
 */
public class UserCancelSubscriptionProcessActionHandler extends ContentTableDeleteDataProcessActionHandler 
{
	public void processAction() throws PortalException
	{
		ContentStringResource resource = new ContentStringResource();
		resource.addResourcePath("lumis/service/mailmarketing/strings/strings");
		
		String [] subscriptionIds = (String [])parameters.get("id");
		
		if(subscriptionIds == null)
		{
			this.source.load();
			TabularData tabularData = this.source.getData();
			
			if(tabularData.getTotalRows() > 0)
			{
				String subscriptionId = tabularData.getRows().get(0).get("id", String.class);
				subscriptionIds = new String[1];
				subscriptionIds[0] = subscriptionId;
			}
			
			if(subscriptionIds == null)
				throw new PortalException("STR_NOT_PREVIOUSLY_SUBSCRIBED", resource);
		}
				
		parameters.put("id", subscriptionIds);

		// now call super to delete the subscription itself
		super.processAction();
	}
}
